package com.masterclient.app.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.MotionEvent;

import com.masterclient.app.BaseActivity;
import com.masterclient.app.BaseFragment;
import java.util.List;

/**
 * @author Hxm on 2019/12/17.
 */
@SuppressLint("Registered")
public class FragmentHostActivity extends BaseActivity {
    private static final String FRAGMENT_NAME = "fragment name";
    private static final String FRAGMENT_BUNDLE = "fragment bundle";
    private static final String FRAGMENT_HASHCODE = "fragment hashcode";
    private static final String ACTIVITY_ORITATION = "activity_oritation";
    private static SparseArray<Fragment> fragmentMap = new SparseArray<>(3);
    private Fragment frg;
    private String fragmentName;

    public static Intent newIntent(Context context, Fragment fragment) {
        Intent it = new Intent(context, FragmentHostActivity.class);
        it.putExtra(FRAGMENT_HASHCODE, fragment.hashCode());
        it.putExtra(FRAGMENT_NAME, fragment.getClass().getName());
        it.putExtra(FRAGMENT_BUNDLE, fragment.getArguments());
        it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        fragmentMap.put(fragment.hashCode(), fragment);
        return it;
    }

    public static void openFragment(Context context, Fragment fragment) {
        context.startActivity(newIntent(context, fragment));
    }

    @Override
    protected void onCreate(Bundle savedInstance) {
//        configStatusBar();
        int key = getIntent().getIntExtra(FRAGMENT_HASHCODE, 0);
        fragmentName = getIntent().getStringExtra(FRAGMENT_NAME);
        frg = fragmentMap.get(key);
        fragmentMap.remove(key);
        if (null == frg) {
            try {
                String fragmentClassName = getIntent().getStringExtra(FRAGMENT_NAME);
                Class fragmentClass = Class.forName(fragmentClassName);
                frg = (Fragment) fragmentClass.newInstance();
                frg.setArguments(getIntent().getBundleExtra(FRAGMENT_BUNDLE));
            } catch (Exception e) {
                e.printStackTrace();
                finish();
                return;
            }
        }
        Bundle bundle = frg.getArguments();
        if (null != bundle) {
            setRequestedOrientation(bundle.getInt(ACTIVITY_ORITATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT));
        }
        super.onCreate(savedInstance);
        addFragmentNeedToStack(frg);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!TextUtils.isEmpty(fragmentName) && fragmentName.contains("PictureMaterialFragment")){
            //过滤掉PictureMaterialFragment(图片查看器页面)的双击事件拦截，因为这个页面的支持图片双击放大
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                onUserInteraction();
            }
            if (getWindow().superDispatchTouchEvent(ev)) {
                return true;
            }
            return onTouchEvent(ev);
        }else {
            return super.dispatchTouchEvent(ev);
        }
    }

    @Override
    public void onBackPressed() {
        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
            super.onBackPressed();
        } else {
            try {
                List<Fragment> fragments = getSupportFragmentManager().getFragments();
                if (null != fragments && !fragments.isEmpty()) {
                    for (Fragment fragment : fragments) {
                        if (null != fragment) {
                            if (fragment.isResumed() && fragment.getUserVisibleHint() && fragment instanceof BaseFragment) {
                                BaseFragment f = (BaseFragment) fragment;
                                if (f.onBackpressed()) {
                                    return;
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            finish();
        }
    }
}

